package com.feidu.fragment;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.actionbarsherlock.app.SherlockFragment;
import com.feidu.R;
import com.feidu.bean.Person;
import com.feidu.em.EMHelperManager;
import com.feidu.utils.CommonUtil;
import com.feidu.utils.MessageID;
import com.hyphenate.exceptions.HyphenateException;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

/**
 * 
 * 生成日期：2016-7-11 <br/>
 * 描述：注册
 */
public class RegisterFragment extends SherlockFragment {

	private EditText edit_name;
	private EditText edit_pword;
	private Button btn_register;

	public RegisterFragment newInstance() {

		RegisterFragment fragment = new RegisterFragment();

		return fragment;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.activity_register, null);
		edit_name = (EditText) view.findViewById(R.id.edit_name);
		edit_pword = (EditText) view.findViewById(R.id.edit_pword);
		btn_register = (Button) view.findViewById(R.id.btn_register);
		btn_register.setOnClickListener(onClickListener);
		return view;
	}

	private OnClickListener onClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			String name = edit_name.getText().toString();
			String pwd = edit_pword.getText().toString();
			boolean isCheck = checkInfo(name, pwd);
			if (isCheck) {

				 saveBomb(name, pwd);
				saveUserInfo(name, pwd);
			}

		}
	};

	private boolean checkInfo(String name, String pwd) {
		if (TextUtils.isEmpty(name) && TextUtils.isEmpty(pwd)) {

			CommonUtil.makeText(getActivity(), "请正确填写信息");

		} else {

			return true;
		}

		return false;

	}

	private ProgressDialog pd;

	/**
	 * 环信注册
	 * 
	 * @param username
	 * @param pwd
	 */
	private void saveUserInfo(final String username,final String pwd){
		pd = new ProgressDialog(getActivity());
		pd.setMessage("正在注册");
		pd.show();
		
		new Thread(new Runnable(){
			
			@Override
			public void run() {

				try {
					
					EMHelperManager.getInstance().registerEM(username, pwd);
					mHandler.sendEmptyMessage(MessageID.EM_REGISTER_SUCCESS);

				} catch (HyphenateException e) {

					int errorCode = e.getErrorCode();
					Message msg = mHandler.obtainMessage();
					msg.what = MessageID.EM_REGISTER_ERROR;
					msg.arg1 = errorCode;
					mHandler.sendMessage(msg);
				}
				
			}
			
		}).start();
		
	}

	private Handler mHandler = new Handler() {

		public void handleMessage(android.os.Message msg) {

			switch (msg.what) {
			case MessageID.EM_REGISTER_SUCCESS:
				CommonUtil.makeText(getActivity(), "注册成功");
				pd.dismiss();
				finish();
				break;
			case MessageID.EM_REGISTER_ERROR:
				int errorCode = msg.arg1;
				CommonUtil.makeText(getActivity(), "注册失败:" + errorCode);
				pd.dismiss();
				break;
			}

		};

	};

	/**
	 * Bomb注册
	 * 
	 * @param name
	 * @param pwd
	 */
	private void saveBomb(String name, String pwd) {
		final Person person = new Person();
		person.setName(name);
		person.setPwd(pwd);
		person.save(new SaveListener() {
			@Override
			public void done(Object o, BmobException e) {
				if(e==null){
					CommonUtil.makeText(getActivity(), "添加数据成功，返回objectId为："
							+ person.getObjectId());
					finish();
				}else{
					CommonUtil.makeText(getActivity(), "创建数据失败：" + e.toString());
				}
			}
		});
	}

	private void finish() {

		this.finish();
	}
}
